package bishe;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

public class mytable {
	JButton ton2 = new JButton("购买");
	JButton ton3 = new JButton("购买");
	double pay = 0;

	public JPanel pay() throws IOException {
		JPanel pan = new JPanel();
		my datapay = new my(getdata.getorder().size(), 4, getdata.getorder());
		JTable table2 = new JTable();// 购物车界面
		table2.setModel(datapay);
		pan.setLayout(null);
		JButton romove = new JButton("移除");
		JButton empty = new JButton("清空");
		JButton refresh = new JButton("刷新");
		JButton payed = new JButton("结账");
		JScrollPane sp = new JScrollPane(table2);
		// 设定按键位置
		sp.setBounds(20, 40, 680, 580);
		romove.setBounds(100, 5, 100, 30);
		empty.setBounds(230, 5, 100, 30);
		refresh.setBounds(360, 5, 100, 30);
		payed.setBounds(490, 5, 100, 30);
		// 监听器
		refresh.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				my data = new my(getdata.getorder().size(), 4, getdata.getorder());
				table2.setModel(data);
			}
		});
		romove.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO 自动生成的方法存根
				int count = table2.getSelectedRow();// 获取你选中的行号（记录）
				String getname = table2.getValueAt(count, 1).toString();
				try {
					order.remove(getname);
					my data = new my(getdata.getorder().size(), 4, getdata.getorder());
					table2.setModel(data);
					JOptionPane.showMessageDialog(pan, "移除成功");
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
		});
		empty.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO 自动生成的方法存根
				try {
					order.empty();
					my data = new my(getdata.getorder().size(), 4, getdata.getorder());
					table2.setModel(data);
					JOptionPane.showMessageDialog(pan, "购物车已清空");
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
		});
		payed.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO 自动生成的方法存根
				my data = new my(getdata.getorder().size(), 4, getdata.getorder());
				table2.setModel(data);
				try {
					String str = null;
					BufferedReader in = new BufferedReader(new FileReader("GoodsList.txt"));
					while ((str = in.readLine()) != null) {
						String[] st = str.split(" ");
						pay = pay + Double.valueOf(st[3]);
					}
					in.close();
					JFrame f = new JFrame("商品结算");
					f.setLayout(new BorderLayout());
					f.setSize(400, 400);
					f.setLocation(300, 200);
					JPanel pan = new JPanel();
					pan.setLayout(null);
					// 文本框初始化
					JTextField b = new JTextField(25);
					b.setBounds(10, 100, 360, 20);
					// 文本域初始化 显示商品价格
					JTextArea price = new JTextArea();
					price.setEditable(false);
					price.setBounds(10, 10, 360, 80);
					// 添加确定按钮
					JButton ton = new JButton("支付");
					ton.setBounds(160, 250, 100, 30);
					price.append(Double.toString(pay) + "\n");
					price.append("请在文本框输入支付金额：" + "\n");
					pan.add(b);
					pan.add(price);
					pan.add(ton);
					f.add(pan);
					f.setUndecorated(false);
					f.setVisible(true);
					ton.addActionListener(new ActionListener() {

						@Override
						public void actionPerformed(ActionEvent arg0) {
							// TODO 自动生成的方法存根
							double numprice = Double.valueOf(b.getText());
							double change = numprice - pay;
							price.append("支付:" + b.getText() + "元      " + " 找零:" +Double.toString(change).substring(0,3) );
						}
					});
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}

			}
		});
		pan.add(payed);
		pan.add(refresh);
		pan.add(romove);
		pan.add(empty);
		pan.add(sp);
		return pan;
	}

	public JPanel read() throws IOException {
		JPanel pan = new JPanel();
		pan.setLayout(null);
		my data = new my(getdata.getreaddata().size(), 4, getdata.getreaddata());
		// 全局表格2 预览
		JTable table = new JTable(data);
		JScrollPane sp = new JScrollPane(table);
		sp.setBounds(20, 30, 680, 595);
		ton2.setBounds(310, 5, 80, 20);
		ton2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				StringBuilder ts = new StringBuilder();
				int count = table.getSelectedRow();// 获取你选中的行号（记录）
				ts.append(table.getValueAt(count, 0) + " ");
				ts.append(table.getValueAt(count, 1) + " ");
				ts.append(table.getValueAt(count, 2) + " ");
				ts.append(table.getValueAt(count, 3));
				try {
					BufferedWriter out = new BufferedWriter(new FileWriter("GoodsList.txt", true));
					out.append(ts);
					out.newLine();
					out.close();
					JOptionPane.showMessageDialog(pan, table.getValueAt(count, 1) + "购买成功 付款前请刷新购物车");
				} catch (IOException e1) {
					// TODO 自动生成的 catch 块
					e1.printStackTrace();
				}

			}
		});
		pan.add(ton2);
		pan.add(sp);
		return pan;
	}

	public JPanel found() throws IOException {
		JPanel pan = new JPanel();
		pan.setLayout(null);
		pan.setPreferredSize(new Dimension(300, 60));
		JLabel bel = new JLabel("商品名称");
		bel.setBounds(80, 5, 80, 20);
		JTextField txt = new JTextField(20);
		txt.setBounds(180, 5, 200, 20);
		JButton ton = new JButton("确认查询");
		ton.setBounds(420, 5, 90, 20);
		ton3.setBounds(550, 5, 90, 20);
		pan.add(bel);
		pan.add(txt);
		pan.add(ton);
		my data = new my(getdata.getfounddata().size(), 4, getdata.getfounddata());
		// 全局表格3 查询
		JTable table3 = new JTable(data);
		JScrollPane scro = new JScrollPane(table3);
		scro.setBounds(20, 30, 680, 595);
		ton3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO 自动生成的方法存根
				StringBuilder ts = new StringBuilder();
				int count = table3.getSelectedRow();// 获取你选中的行号（记录）
				ts.append(table3.getValueAt(count, 0) + " ");
				ts.append(table3.getValueAt(count, 1) + " ");
				ts.append(table3.getValueAt(count, 2) + " ");
				ts.append(table3.getValueAt(count, 3));
				try {
					BufferedWriter out = new BufferedWriter(new FileWriter("GoodsList.txt", true));
					out.append(ts);
					out.newLine();
					out.close();
					JOptionPane.showMessageDialog(pan, table3.getValueAt(count, 1) + "购买成功 付款前请刷新购物车");
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}

			}
		});
		ton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					if (txt.getText().length() == 0) {
						JOptionPane.showMessageDialog(pan, "名称不能为空");
						// 名称输入框获取焦点
						txt.grabFocus();
						return;
					} else {
						String str;
						BufferedWriter out = new BufferedWriter(new FileWriter("foundtemp.txt"));
						BufferedReader in = new BufferedReader(new FileReader("Goods.txt"));
						while ((str = in.readLine()) != null) {
							if (str.matches("(.*)" + txt.getText() + "(.*)")) {
								out.append(str);
								out.newLine();
							}
						}
						in.close();
						out.close();
						my data = new my(getdata.getfounddata().size(), 4, getdata.getfounddata());
						table3.setModel(data);
					}
				} catch (IOException e1) {
					// TODO 自动生成的 catch 块
					e1.printStackTrace();
				}
			}
		});
		pan.add(ton3);
		pan.add(scro);
		return pan;
	}

	class my extends AbstractTableModel {
		/**
		 * 
		 */
		private static final long serialVersionUID = -5928036984233054961L;
		int rowIndex, columnIndex;
		ArrayList<String> list;
		String[] name = { "id", "name", "size", "price" };

		public my() {

		}

		public my(int rowindex, int col, ArrayList<String> list) {
			this.rowIndex = rowindex;
			this.columnIndex = col;
			this.list = list;
		}

		@Override
		public int getColumnCount() {
			// TODO 自动生成的方法存根
			return columnIndex;
		}

		@Override
		public String getColumnName(int columnIndex) {
			// TODO 自动生成的方法存根
			return name[columnIndex];
		}

		@Override
		public int getRowCount() {
			// TODO 自动生成的方法存根
			return rowIndex;
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			// TODO 自动生成的方法存根
			String[] st = list.get(rowIndex).split(" ");
			if (columnIndex == 0) {
				return st[0];
			} else if (columnIndex == 1) {
				return st[1];
			} else if (columnIndex == 2) {
				return st[2];
			} else
				return st[3];
		}

		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			// TODO 自动生成的方法存根

			return false;
		}
	}
}

class getdata {
	// 预览界面获取数据
	public static ArrayList<String> getreaddata() throws IOException {
		String str;
		ArrayList<String> list = new ArrayList<String>();
		BufferedReader in = new BufferedReader(new FileReader("Goods.txt"));
		while ((str = in.readLine()) != null) {
			list.add(str);
		}
		in.close();
		return list;
	}

//查询界面获取数据
	public static ArrayList<String> getfounddata() throws IOException {
		ArrayList<String> list = new ArrayList<String>();
		String str = null;
		BufferedReader in = new BufferedReader(new FileReader("foundtemp.txt"));
		while ((str = in.readLine()) != null) {
			list.add(str);
		}
		in.close();
		return list;
	}

//购物车获取数据
	public static ArrayList<String> getorder() {
		ArrayList<String> list = new ArrayList<String>();
		String str = null;
		BufferedReader in;
		try {
			in = new BufferedReader(new FileReader("GoodsList.txt"));

			while ((str = in.readLine()) != null) {
				list.add(str);
			}
			in.close();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return list;

	}
}